package com.community.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.community.dto.StorageGatherDto;
import com.community.mapper.GatherDetailsMapper;
import com.community.pojo.Gather;
import com.community.mapper.GatherMapper;
import com.community.pojo.GatherDetails;
import com.community.service.IGatherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.community.utils.PageUtils;
import com.community.vo.StorageGatherTwoVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author TangHaoSong Fly ChenJie HeYuHang
 * @since 2024-05-15
 */
@Service
public class GatherServiceImpl extends ServiceImpl<GatherMapper, Gather> implements IGatherService {

    @Autowired
    private GatherMapper gatherMapper;

    @Autowired
    private GatherDetailsMapper gatherDetailsMapper;

    @Override
    public PageUtils<Gather> updateAudit() {
        QueryWrapper<Gather> qw=new QueryWrapper<>();
        qw.eq("CHECK_TAG", "S001-1");
        List<Gather> list = gatherMapper.selectList(qw);
        PageInfo<Gather> p=new PageInfo<>(list);
        PageUtils<Gather> utils=new PageUtils<>();
        utils.setTotal(p.getTotal());
        utils.setRows(p.getList());
        return utils;
    }

    @Override
    public String getMaxgatherId() {
        Gather gather = gatherMapper.queryMaxModule();
        if(gather==null){
            return null;
        }
        String designId = gather.getGatherOrderNumber();
        return designId;
    }
//    @Override
//    public void page1(StorageGatherDto dto) {
//        this.save(dto);
//        List<GatherDetails> details = dto.getDetails();
//        for(int i=0;i<details.size();i++){
//            GatherDetails gatherDetails = details.get(i);
//            //每条id明细设置为null
//            gatherDetails.setId(null);
//            gatherDetails.setGatherId(Integer.valueOf((dto.getGatherId())));
////            gatherDetails.setGatherId(dto.setGatherId(new BigInteger(typeNumber)));
////            storageGatherDetails.setGatherTag("K002-1");
//            BigDecimal num=new BigDecimal(0);
//            gatherDetails.setAmount(num);
//            gatherDetailsMapper.insert(gatherDetails);
//        }
//    }
    @Override
    public void page1(StorageGatherDto dto) {
//        this.save(dto);
//        List<GatherDetails> details = dto.getDetails();
//        if (details != null) {
//            for (GatherDetails gatherDetails : details) {
//                // 每条明细设置id为null
//                gatherDetails.setId(null);
//
//                // 设置每条明细的入库单编号
//                gatherDetails.setGatherId(Integer.valueOf(dto.getGatherId()));
//
//                // 设置每条明细的数量为0
//                BigDecimal num = new BigDecimal(0);
//                gatherDetails.setAmount(num);
//
//                // 插入每条明细到数据库
//                gatherDetailsMapper.insert(gatherDetails);
//            }
//        } else {
//            // 如果details为null，可以根据实际需求添加错误处理或日志记录
//            System.out.println("Details is null. Unable to process further.");
//        }
    }

    @Override
    public PageUtils<Gather> twoShow(StorageGatherTwoVo vo, int pageNo, int pageSize) {
        QueryWrapper<Gather> qw=new QueryWrapper<>();
        qw.eq("CHECK_TAG","S001-3");
        //设置状态为已登记
        //qw.eq("STORE_TAG","K002-1");
        if(vo!=null){
            if(!StringUtils.isEmpty(vo.getReason())){
                qw.eq("REASON",vo.getReason());
            }
            if (!StringUtils.isEmpty(vo.getSearch_registerTime1())) {
                qw.ge("register_time", vo.getSearch_registerTime1());
            }
            if (!StringUtils.isEmpty(vo.getSearch_registerTime2())) {
                qw.le("register_time", vo.getSearch_registerTime2());
            }
        }
        PageHelper.startPage(pageNo, pageSize);
        List<Gather> list = gatherMapper.selectList(qw);
        PageInfo<Gather> p=new PageInfo<>(list);
        PageUtils<Gather> utils=new PageUtils<>();
        utils.setTotal(p.getTotal());
        utils.setRows(p.getList());
        return utils;
    }

}
